home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 326-350 / disk_333 / multiplot / source / mplot_src / getdat.c < prev    next >
C/C++ Source or Header  |  1992-05-06  |  7KB  |  243 lines

  1. #include <stdio.h>
  2. #include <ctype.h>
  3. #include <exec/types.h>
  4. #include <exec/memory.h>
  5. #include <intuition/intuition.h>
  6. #include "mp.h"
  7. #include "struct.h"
  8.  
  9. #define MAXCOLUMNS 10
  10.  
  11. extern int debug;
  12.  
  13. extern char *getwrd();
  14. extern void trch();
  15. extern int same(), numeric();
  16. extern struct Window *FrontWindow;
  17. extern struct NewScreen newscreen;
  18. struct Plot *GetStructPlot();
  19.  
  20. /***************************************************************************
  21. *     GetDat finds numeric data in a text file and stores it in arrays.
  22. *
  23. *     GetDat reads a text file whose handle is pointed to by 'fp'.  In
  24. *  this file are expected to be lines containing "words" [of arbitrary
  25. *  length, delimited by blanks, tabs, or commas.] In each line, the first
  26. *  word will be pointed to by 'wp[0]'; the user thinks of this
  27. *  word as being in column 1.  'wp[1]' will point to the next word,
  28. *  etc.  GetDat is primarily interested in those words which look like
  29. *  numbers.
  30. *
  31. *     The user has assigned column numbers to the variables x, y, and e.
  32. *  She wants GetDat to find all the lines which contain numbers in those
  33. *  columns and save the numbers in the arrays 'x[]', 'y[]', 'e[]'.
  34. *  Further, successive lines which contain numbers in the columns of
  35. *  interest are to be treated as a list [possibly delimited by lines
  36. *  which don't contain numbers in the columns of interest.]
  37. *  Finally, the number of lists and the numbers of lines
  38. *  in each list are to be saved in the data structure pointed to by the
  39. *  argument 'Pict'.
  40. *
  41. *     Special handling: If the column associated with 'x[]' is given as 0,
  42. *  the 'x[]' array is to be filled as if the x-columns of each list
  43. *  contained [0,1,2...].  If the column associated with 'e[]' is 0, the
  44. *  'e[]' array is not to be filled at all.
  45. *
  46. *     Lines which begin with "*SCALFACT*" contain scale factors by which
  47. *  collected data is to be multiplied.  Scale factor columns map to data
  48. *  columns after the keyword "*SCALFACT*" is discarded.
  49. *
  50. *     Lines which begin with "*TITLE*", "*XLABEL*", and "*YLABEL*" are
  51. *  interpreted as text to be stored in the structure pointed to by
  52. *  'Pict'.  The keyword "*...*" and the blanks, tabs, or commas following
  53. *  it are discarded.
  54. *
  55. *************************************/
  56.  
  57. #define COLUMN_OK(z) (z<0 ? TRUE : numeric(wp[z]))
  58. #define LINE_IS_DATA (COLUMN_OK(xcol) && COLUMN_OK(ycol) && COLUMN_OK(ecol))
  59. #define DATA(z) (fact[(z)]*atoFFP(wp[(z)]))
  60. #define PLOT_OPENED (Plot->NPts && !PlotClosed)
  61.  
  62. FFP fact[MAXCOLUMNS];                /* SCALE FACTORS */
  63. short maxcol;    /* LARGEST OF ARGS: XCOL, YCOL, ECOL */
  64. char title[80], xlabel[80], ylabel[80];
  65.  
  66. /********************************************/
  67. void GetDat(fp, xcol, ycol, ecol, terse, Pict)
  68. FILE *fp;
  69. int xcol, ycol, ecol;   /* column numbers to be inspected */
  70. int terse;              /* false => echo non-numeric lines to stdout */
  71. struct Pict *Pict;      /* contains info for plotting this data */
  72. {
  73.    short i, PlotClosed = FALSE, Continued = FALSE;
  74.    short xcoord = 0;
  75.    char cbuf[100], *cb, *wp[MAXCOLUMNS];
  76.    struct Plot *Plot, *PrevPlot;
  77.    FFP *x, *y, *e;
  78.  
  79. /*** INITIALIZE ***/
  80.  
  81.    /* TRANSLATE from user column numbers to array indices, FIND max */
  82.    xcol--; ycol--; ecol--; maxcol = max(xcol,max(ycol,ecol));
  83.    for (i=0; i<=maxcol; i++) fact[i] = 1.;   /* init scale factors */
  84.    Pict->NPlt = 0;
  85.    Pict->Plot = GetStructPlot();
  86.    Plot = Pict->Plot;
  87.    x = Plot->x; y = Plot->y; e = Plot->e;
  88.  
  89. /*** GET DATA ***/
  90.  
  91.    /* FIND list(s) of numbers in file, GET numbers in x,y(,e) arrays */
  92.    while (cb = fgets(cbuf,100,fp)) {
  93.  
  94.       trch('\n',0,cb); trch('\t',' ',cbuf); trch(',',' ',cbuf); trch('\r',' ',cbuf);
  95.  
  96.       if (!KeyWord(cb,Pict)) {
  97.  
  98.          for (i=0; i<=maxcol; i++) wp[i] = getwrd(&cb);
  99.  
  100.          if (LINE_IS_DATA) {
  101.             if (PlotClosed) {
  102.                /* allocate, link, SET UP FOR NEW PLOT */
  103.                Plot->NextPlot = GetStructPlot(); Plot = Plot->NextPlot;
  104.                x = Plot->x; y = Plot->y; e = Plot->e;
  105.                PlotClosed = FALSE; xcoord = 0;
  106.             }
  107.             else if (Plot->NPts == MAXPOINTS) {
  108.                /* allocate, link, SET UP FOR CONTINUATION OF CURRENT PLOT */
  109.                Continued = TRUE; PrevPlot = Plot;
  110.                Plot->NextPlot = GetStructPlot(); Plot = Plot->NextPlot;
  111.                x = Plot->x; y = Plot->y; e = Plot->e;
  112.             }
  113.             else if (Continued)
  114.                {PrevPlot->Continued = TRUE; Continued = FALSE;}
  115.  
  116.             /*** PAYLOAD ***/
  117.             /* STORE data in arrays, COUNT points */
  118.             *(x++) = ( xcol>=0 ? DATA(xcol) : (FFP)(xcoord++));
  119.             *(y++) = DATA(ycol);
  120.             if (ecol>=0) *(e++) = DATA(ecol);
  121.             Plot->NPts++;
  122.          }
  123.  
  124.          else {
  125.             /* line does not contain plottable data */
  126.             if (PLOT_OPENED) {
  127.                Pict->NPlt++;
  128.                PlotClosed = TRUE;
  129.             }
  130.          }
  131.       }
  132.    }
  133.  
  134.    if (PLOT_OPENED) Pict->NPlt++;
  135.    return;
  136. }
  137.  
  138. int CheckDat(fp, xcol, ycol, ecol)
  139. FILE *fp;
  140. int xcol, ycol, ecol;   /* column numbers to be inspected */
  141. {
  142. char cbuf[100];
  143. int i=0;
  144.    maxcol = max(xcol,max(ycol,ecol));
  145.  
  146.    while ( fgets(cbuf,99,fp) && ( (i==0) || (i>=maxcol) ) )
  147.        {
  148.           strcat(cbuf,'\0');
  149.           i=countwords(&cbuf);
  150.        }
  151.     if ( (i<maxcol) && (i!=0) )
  152.       {
  153.          SetWindowTitles(FrontWindow,-1,newscreen.DefaultTitle);
  154.          Message("   Not enough columns in file   ");
  155.          return(FALSE);
  156.       }
  157.   return(TRUE);
  158. }
  159.  
  160. countwords(s)
  161. char *s;
  162. {
  163. int x=0;
  164.  
  165.  
  166.   while ((*s !='\0')&&(!isdigit(*s)))
  167.     {
  168.        if (isalpha(*s))   return(0);
  169.        s++;
  170.     }
  171.  
  172.   while (*s !='\0')
  173.     {
  174.        if (isdigit(*s))
  175.          {
  176.             x++;
  177.             while (isalnum(*s)||ispunct(*s))  s++;
  178.          }
  179.        else s++;
  180.      }
  181.    return(x);
  182. }
  183.  
  184. /******************/
  185. int KeyWord(cb,Pict)
  186. char *cb;
  187. struct Pict *Pict;
  188. {
  189.    short i;
  190.  
  191.    if (same(cb,"*",1)) {
  192.       if (same(cb,"*SCALFACT*",10)) {
  193.          (void)getwrd(&cb);
  194.          for (i=0; i<=maxcol; i++) {
  195.             fact[i] = atoFFP(getwrd(&cb));
  196.          }
  197.          return (TRUE);
  198.       }
  199.       else if (same(cb,"*TITLE*",7)) {
  200.          (void)getwrd(&cb); (void)strcpy(title,cb);
  201.          Pict->Title = title;
  202.          return (TRUE);
  203.       }
  204.       else if (same(cb,"*XLABEL*",8)) {
  205.          (void)getwrd(&cb); (void)strcpy(xlabel,cb);
  206.          Pict->XLabel = xlabel;
  207.          return (TRUE);
  208.       }
  209.       else if (same(cb,"*YLABEL*",8)) {
  210.          (void)getwrd(&cb); (void)strcpy(ylabel,cb);
  211.          Pict->YLabel = ylabel;
  212.          return (TRUE);
  213.       }
  214.    }
  215.    return (FALSE);
  216. }
  217.  
  218.  
  219. /*************************************************************************/
  220. struct Remember *Key=NULL;
  221.  
  222. struct Plot *GetStructPlot()
  223. {
  224.    struct Plot *P;
  225.  
  226.    P     = (struct Plot *)AllocRemember(&Key,sizeof(struct Plot),MEMF_CLEAR);
  227.    P->x  = (FFP *)AllocRemember(&Key,MAXPOINTS * sizeof(FFP),MEMF_CLEAR);
  228.    P->y  = (FFP *)AllocRemember(&Key,MAXPOINTS * sizeof(FFP),MEMF_CLEAR);
  229.    P->e  = (FFP *)AllocRemember(&Key,MAXPOINTS * sizeof(FFP),MEMF_CLEAR);
  230.    P->xp = (short *)AllocRemember(&Key,MAXPOINTS * sizeof(short),MEMF_CLEAR);
  231.    P->yp = (short *)AllocRemember(&Key,MAXPOINTS * sizeof(short),MEMF_CLEAR);
  232.    P->ep = (short *)AllocRemember(&Key,MAXPOINTS * sizeof(short),MEMF_CLEAR);
  233.    P->Reg = (struct PlotRegion *) AllocRemember(&Key,sizeof(struct PlotRegion),MEMF_CLEAR);
  234.    return(P);
  235. }
  236.  
  237.  
  238. /*******************/
  239. void FreeStructPlot()
  240. {
  241.    FreeRemember(&Key,TRUE);
  242. }
  243.